package com.sx.basic;

import com.sx.utils.PasswordUtils;
import com.sx.utils.RandomUtils;
import lombok.Data;

/**
 * @author AdinZ
 * @date 2020/11/7 10:16
 */
@Data
public class TokenEntity<T> extends BasicEntity {
    public static final String TOKEN="TOKEN";
    public static final String TOKEN_VALUE="TOKEN_VALUE";
    private static final long serialVersionUID = 112544L;

    private String token;
    private String ip;
    private long created;
    private Type type;
    private long uid;
    private long expire;
    private Channel channel;
    private T data;
    public enum Type{
        USER,SYSTEM
    }
    public enum  Channel{
        APP_ANDROID,
        APP_IOS,
        H5,

        PC,
        //公众号
        WX_OFFIACCOUNT,
        //小程序
        WX_MINI_PROGRAM;
    }


    public TokenEntity(long uid,Type type,Channel channel,T data,long expire) {
        this.created = System.currentTimeMillis();
        this.expire = expire;
        this.type = type;
        this.uid = uid;
        this.channel = channel;
        this.data = data;
        String uid_encode = this.getTokenStart();
        try {
            this.token = uid_encode+PasswordUtils.getInstance().aes_encrypt_base64_url(RandomUtils.randomSnowflake("") + this.created + "_" + uid);
        } catch (Exception exception) {
            this.token = uid_encode+RandomUtils.randomStr(128);
        }
    }
    public String getTokenStart(){
        String uid_encode = PasswordUtils.getInstance().base64_url_encode(uid+"_" + this.type+"_"+this.channel);
        return uid_encode+"__";
    }



}
